﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PServiceBus.Core.Runtime {
    /// <summary>
    /// Define a generic equality comparer for comparing object using distinct, e.t.c
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class FuncEqualityComparer<T> : IEqualityComparer<T> {
        readonly Func<T, T, bool> _comparer;
        readonly Func<T, int> _hash;

        /// <summary>
        /// Setup a new equality comparer
        /// </summary>
        /// <param name="comparer"></param>
        /// <param name="hash"></param>
        public FuncEqualityComparer(Func<T, T, bool> comparer, Func<T, int> hash) {
            _comparer = comparer;
            _hash = hash;
        }

        /// <summary>
        /// Return true based on the defined func comparer
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Equals(T x, T y) {
            return _comparer(x, y);
        }

        /// <summary>
        /// Return the specific hash code for the object
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int GetHashCode(T obj) {
            return _hash(obj);
        }
    }
}
